package cn.tedu.myvrd.controller;

import cn.tedu.myvrd.entity.Product;
import cn.tedu.myvrd.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("send")
    public void send(Product product, MultipartFile file){
        //得到文件的后缀名
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        System.out.println("product = " + product + ", file = " + file);
        SimpleDateFormat f =new SimpleDateFormat("/yyyy/MM/dd/");
        //按照当天的日期进行转换
        String dataPath = f.format(new Date());
        String path = "D:/upload"+dataPath;
        File direFile = new File(path);
        if(!direFile.exists()){
            direFile.mkdirs();//创建文件夹
        }
        System.out.println("path:"+path);
        try{
            //把文件保存到指定的文件夹
            file.transferTo(new File(path+fileName));
        }catch(IOException e){
            e.printStackTrace();
        }
        //把文件路径保存到对象中
        product.setImgurl(dataPath+fileName);
        //设置图片创建时间 为当前系统时间 new Date()得到就是当前时间对象
        product.setCreated(new Date());
        //把对象中的数据保存到数据库中
        mapper.insert(product);
    }
    @RequestMapping("/productlist")
    List<Product> list(){
        return mapper.selectAll();
    }
    @RequestMapping("/viewlist")
    public List<Product> viewList(){
        return mapper.selectViewList();
    }

    @RequestMapping("/likelist")
    public List<Product> likeList(){
        return mapper.selectLikeList();
    }
    @RequestMapping("/select")
    public Product selectById(int id,HttpSession session){
        //        取出之前保存在session中的数据 如果有值说明浏览过
        String viewId=(String) session.getAttribute("view"+id);
        if (viewId==null) {//说明没有浏览过
            session.setAttribute("view"+id,id+"");
            //让浏览量+1
            mapper.viewById(id);
        }
        return mapper.selectById(id);
    }
    @RequestMapping("/delete")
    public void deleteById(int id){
        //获取图片路径
        Product product=mapper.selectById(id);
        String path="D:/upload"+product.getImgurl();
//        删除文件
        new File(path).delete();
        mapper.deleteById(id);
    }
    @RequestMapping("/like")
    public Product likeById(int id, HttpSession session){
//        取出之前保存在session中的数据 如果有值说明点过赞
        String likeId=(String) session.getAttribute("like"+id);
        if (likeId==null) {//说明没有点过赞
            session.setAttribute("like"+id,id+"");
            mapper.likeById(id);
        }
        return mapper.selectById(id);
    }
    @RequestMapping("/productlistById")
    public List<Product>listById(int cid){
        return mapper.listById(cid);
    }
    @RequestMapping("/search")
    public List<Product>searchByWord(String wd){
        return mapper.searchByWord(wd);
    }
}
